package com.niodata.dp.core.service;

import com.niodata.dp.core.security.ServiceAuthMapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SystemServiceManager {

  @Autowired
  private ServiceInfoMapper serviceInfoMapper;

  @Autowired
  private ServiceAuthMapper serviceAuthMapper;

  @Transactional
  public void enableSystemServices(List<SystemService> serviceList) {
    for (SystemService service : serviceList) {
      ServiceInfo serviceInfo = new ServiceInfo();
      serviceInfo.setEnabled(true);
      serviceInfo.setDescription(service.getDescription());
      serviceInfo.setName(service.getName());
      serviceInfoMapper.updateServiceInfo(serviceInfo);
    }
  }

  @Transactional
  public void disableSystemServices(List<SystemService> serviceList) {
    for (SystemService service : serviceList) {
      ServiceInfo serviceInfo = new ServiceInfo();
      serviceInfo.setEnabled(false);
      serviceInfo.setDescription(service.getDescription());
      serviceInfo.setName(service.getName());
      serviceInfoMapper.updateServiceInfo(serviceInfo);
      serviceAuthMapper.deleteAuthOfService(service.getName());
    }
  }

  @Transactional
  public void disableSystemService(SystemService service) {
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setEnabled(false);
    serviceInfo.setDescription(service.getDescription());
    serviceInfo.setName(service.getName());
    serviceInfoMapper.updateServiceInfo(serviceInfo);
    serviceAuthMapper.deleteAuthOfService(service.getName());
  }

  public List<SystemService> getEnabledServices() {
    List<ServiceInfo> serviceInfos = serviceInfoMapper.getAllSystemServices();
    List<SystemService> serviceList = new ArrayList<>();
    for (ServiceInfo serviceInfo : serviceInfos) {
      if (serviceInfo.isEnabled()) {
        serviceList.add(SystemServices.getSystemService(serviceInfo.getName()));
      }
    }
    return serviceList;
  }


  @Transactional
  public void addSystemService(SystemService service) {
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setName(service.getName());
    serviceInfo.setEnabled(false);
    serviceInfo.setDescription(service.getDescription());
    this.serviceInfoMapper.addServiceInfo(serviceInfo);
  }

  @Transactional
  public void addSystemService(List<SystemService> services) {
    for (SystemService service : services) {
      ServiceInfo serviceInfo = new ServiceInfo();
      serviceInfo.setName(service.getName());
      serviceInfo.setEnabled(false);
      serviceInfo.setDescription(service.getDescription());
      this.serviceInfoMapper.addServiceInfo(serviceInfo);
    }
  }

  @Transactional
  public void deleteSystemService(SystemService service) {
    serviceInfoMapper.removeService(service.getName());
    serviceAuthMapper.deleteAuthOfService(service.getName());
  }

}
